/**
 * 事件续报模块服务
 */

// 模拟延迟
const delay = (ms = 1000) => new Promise(resolve => setTimeout(resolve, ms));

// 模拟事件续报数据
const mockEventReports = [
  {
    id: 1,
    eventName: '某市突发急性传染病疫情',
    eventType: '传染病疫情',
    occurTime: '2024-11-01 08:30',
    location: '某市东城区',
    status: '持续处理中',
    totalCases: 100,
    newCases: 5,
    treatmentCount: 20,
    curedCount: 75,
    deathCount: 5,
    newDeathCount: 0,
    closeContactCount: 150,
    subCloseContactCount: 450,
    reporter: '卫生监督员陈主任',
    reportTime: '2024-11-01 09:00',
    progress: 75
  },
  {
    id: 2,
    eventName: '学校食物中毒事件',
    eventType: '食品安全事件',
    occurTime: '2024-10-28 12:00',
    location: '某市第三中学',
    status: '基本控制',
    totalCases: 45,
    newCases: 0,
    treatmentCount: 5,
    curedCount: 40,
    deathCount: 0,
    newDeathCount: 0,
    closeContactCount: 200,
    subCloseContactCount: 0,
    reporter: '教育局督察员王科长',
    reportTime: '2024-10-28 14:00',
    progress: 90
  },
  {
    id: 3,
    eventName: '工厂化学品泄漏事故',
    eventType: '环境污染',
    occurTime: '2024-10-25 15:30',
    location: '某工业园区',
    status: '完全控制',
    totalCases: 12,
    newCases: 0,
    treatmentCount: 0,
    curedCount: 12,
    deathCount: 0,
    newDeathCount: 0,
    closeContactCount: 50,
    subCloseContactCount: 0,
    reporter: '环保局监测员周工程师',
    reportTime: '2024-10-25 16:00',
    progress: 100
  }
];

// 模拟存储记录数据
const mockStorageRecords = [
  {
    id: 1,
    eventName: '某市突发急性传染病疫情',
    reporter: '卫生监督员陈主任',
    storageTime: '2024-11-06 10:30:00',
    size: '2.5MB',
    location: '云端存储',
    status: '已存储',
    storageLocation: '/data/reports/2024/11/event_001.dat',
    md5: 'a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6'
  },
  {
    id: 2,
    eventName: '学校食物中毒事件',
    reporter: '教育局督察员王科长',
    storageTime: '2024-10-28 16:20:00',
    size: '1.8MB',
    location: '云端存储',
    status: '已存储',
    storageLocation: '/data/reports/2024/10/event_002.dat',
    md5: 'b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7'
  },
  {
    id: 3,
    eventName: '工厂化学品泄漏事故',
    reporter: '环保局监测员周工程师',
    storageTime: '2024-10-25 18:00:00',
    size: '3.2MB',
    location: '云端存储',
    status: '已存储',
    storageLocation: '/data/reports/2024/10/event_003.dat',
    md5: 'c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8'
  }
];

/**
 * 保存事件续报信息
 */
export function saveEventReport(data) {
  return delay(1500).then(() => {
    console.log('保存事件续报信息:', data);
    return {
      success: true,
      message: '保存成功',
      id: Date.now()
    };
  });
}

/**
 * 保存事件描述及措施
 */
export function saveEventDescription(data) {
  return delay(1500).then(() => {
    console.log('保存事件描述及措施:', data);
    return {
      success: true,
      message: '保存成功'
    };
  });
}

/**
 * 保存处理情况反馈
 */
export function saveHandlingFeedback(data) {
  return delay(1500).then(() => {
    console.log('保存处理情况反馈:', data);
    return {
      success: true,
      message: '保存成功'
    };
  });
}

/**
 * 保存报告人信息
 */
export function saveReporterInfo(data) {
  return delay(1500).then(() => {
    console.log('保存报告人信息:', data);
    return {
      success: true,
      message: '保存成功'
    };
  });
}

/**
 * 校验事件续报信息
 */
export function validateEventReport() {
  return delay(2000).then(() => {
    return {
      success: true,
      message: '校验完成',
      results: {
        passed: 6,
        failed: 0,
        warning: 2
      }
    };
  });
}

/**
 * 获取存储记录列表
 */
export function getStorageRecords() {
  return delay(800).then(() => {
    return [...mockStorageRecords];
  });
}

/**
 * 删除存储记录
 */
export function deleteStorageRecord(id) {
  return delay(1000).then(() => {
    console.log('删除存储记录:', id);
    return {
      success: true,
      message: '删除成功'
    };
  });
}

/**
 * 数据备份
 */
export function backupData() {
  return delay(2000).then(() => {
    return {
      success: true,
      message: '备份成功',
      fileName: `backup_${Date.now()}.zip`,
      size: '15.6MB'
    };
  });
}

/**
 * 查询事件续报
 */
export function queryEventReports(params) {
  return delay(1200).then(() => {
    console.log('查询参数:', params);
    
    let results = [...mockEventReports];
    
    // 按事件名称筛选
    if (params.eventName) {
      results = results.filter(r => r.eventName.includes(params.eventName));
    }
    
    // 按事件类型筛选
    if (params.eventType) {
      results = results.filter(r => r.eventType === params.eventType);
    }
    
    // 按地点筛选
    if (params.location) {
      results = results.filter(r => r.location.includes(params.location));
    }
    
    // 按报告人筛选
    if (params.reporter) {
      results = results.filter(r => r.reporter.includes(params.reporter));
    }
    
    // 按状态筛选
    if (params.status) {
      results = results.filter(r => r.status === params.status);
    }
    
    return results;
  });
}

/**
 * 导出事件续报
 */
export function exportEventReports(data) {
  return delay(2000).then(() => {
    console.log('导出数据:', data);
    
    const timestamp = new Date().getTime();
    const formatMap = {
      'Excel': 'xlsx',
      'PDF': 'pdf',
      'Word': 'docx',
      'Text': 'txt'
    };
    
    const ext = formatMap[data.format] || 'txt';
    const fileName = `事件续报导出_${timestamp}.${ext}`;
    
    return {
      success: true,
      message: '导出成功',
      fileName: fileName,
      downloadUrl: `/download/${fileName}`
    };
  });
}

/**
 * 获取事件跟踪信息
 */
export function getEventTracking(eventId) {
  return delay(1000).then(() => {
    return {
      timeline: [
        {
          id: 1,
          title: '事件初次报告',
          time: '2024-11-01 09:00',
          description: '接到群众报告，多人出现发热、咳嗽等症状',
          status: 'completed',
          details: [
            { label: '涉及人数', value: '50人' },
            { label: '初步诊断', value: '疑似传染病' }
          ],
          reporter: '卫生监督员陈主任'
        },
        {
          id: 2,
          title: '应急响应启动',
          time: '2024-11-01 10:30',
          description: '启动突发公共卫生事件应急预案，成立应急指挥部',
          status: 'completed',
          details: [
            { label: '响应级别', value: '二级' },
            { label: '调配人员', value: '30人' }
          ],
          reporter: '应急指挥部'
        },
        {
          id: 3,
          title: '现场流行病学调查',
          time: '2024-11-02 08:00',
          description: '组织专家开展现场流行病学调查，采集样本送检',
          status: 'completed',
          details: [
            { label: '调查人员', value: '15人' },
            { label: '采集样本', value: '80份' }
          ],
          reporter: '疾控中心调查组'
        },
        {
          id: 4,
          title: '病原体确认',
          time: '2024-11-03 14:00',
          description: '实验室检测结果确认病原体，制定针对性防控方案',
          status: 'completed',
          details: [
            { label: '病原体', value: '甲型流感病毒' },
            { label: '确诊病例', value: '75例' }
          ],
          reporter: '检验科主任'
        },
        {
          id: 5,
          title: '全面防控措施实施',
          time: '2024-11-04 08:00',
          description: '实施隔离治疗、密接追踪、环境消杀等综合防控措施',
          status: 'processing',
          details: [
            { label: '隔离人数', value: '100人' },
            { label: '密接追踪', value: '150人' },
            { label: '消杀面积', value: '5000㎡' }
          ],
          reporter: '防控工作组'
        },
        {
          id: 6,
          title: '疫情趋势分析',
          time: '2024-11-05 16:00',
          description: '分析疫情发展趋势，调整防控策略',
          status: 'processing',
          details: [
            { label: '新增病例', value: '5例' },
            { label: '治愈出院', value: '45例' },
            { label: '疫情趋势', value: '逐步控制' }
          ],
          reporter: '专家组'
        },
        {
          id: 7,
          title: '持续监测观察',
          time: '2024-11-06 当前',
          description: '继续加强监测，巩固防控成果',
          status: 'current',
          details: [
            { label: '在治人数', value: '20人' },
            { label: '观察人数', value: '30人' }
          ],
          reporter: '监测组'
        }
      ]
    };
  });
}

/**
 * 获取事件列表
 */
export function getEventList() {
  return delay(800).then(() => {
    return [...mockEventReports];
  });
}

export default {
  saveEventReport,
  saveEventDescription,
  saveHandlingFeedback,
  saveReporterInfo,
  validateEventReport,
  getStorageRecords,
  deleteStorageRecord,
  backupData,
  queryEventReports,
  exportEventReports,
  getEventTracking,
  getEventList
};

